package cluster

import (
	"github.com/astaxie/beego"
	"open_manjing/microsys/encryption"
	"open_manjing/microsys/jsonio"
)

//App struct.
type App struct {
	beego.Controller
}

//Prepare.
func (c *App) Prepare() {
	if beego.AppConfig.String("runmode") == "prod" {
		key := []byte(beego.AppConfig.String("cluster.key"))
		iv := []byte(beego.AppConfig.String("cluster.iv"))
		rb, err := encryption.AesDecrypt(string(c.Ctx.Input.RequestBody), key, iv)
		if err != nil {
			c.Data["json"] = jsonio.Fail(jsonio.ErrIllegalRequest, err.Error(), nil)
			c.ServeJSON()
			c.StopRun()
			return
		}
		c.Ctx.Input.RequestBody = rb
	}
}

//Finish.
func (c *App) Finish() {
	c.ServeJSON()
}
